require "player"
require "AnAL"
require "tilemap"
require "magnet"
require "ball"

-- Store the screenwidth and height as a global
screenW = 800
screenH = 600

-- Store the location of the camera, if we have scrolled 500px to the right
-- then cameraX is 500.
cameraX = 0
cameraY = 0

-- Store state
state = 0

mouseDown = false
magnetBallExists =false


-- Initilization
function love.load()
	-- Store graphics
	menu = love.graphics.newImage( "Content/startscreencontro.png" )
	gameover = love.graphics.newImage( "Content/gameover.png" )
	
end

function initLoadingState()
	cameraX = 0
	cameraY = 0

	-- Init physics
	world = love.physics.newWorld(-9600, -600, 9600, 600)
	world:setGravity(0, 981)
	world:setMeter(80)
	world:setCallbacks(add, persist, rem, result)	
	bodies = {}
	shapes = {}

	-- Init sky graphic
	sky = love.graphics.newImage( "Content/environment/sky.png" )
	-- Init map
	initMap()
	
	-- Init the magnet
	initMagnet()
	initBall()
	
	-- Init for our player
	loadPlayer()
	
	love.physics.newPulleyJoint(bodies[8], bodies[9], 764, 519, 740, 262, 580, 240, 580, 522, 1.0)

	state = 2
end

-- Something that is called everytime something updates like screen size,
-- time passes etc...
function love.update(dt)
	if state == 0 then
		if love.mouse.isDown( "l" ) then
			if love.mouse.getX() > 260 and love.mouse.getX() < 545
				and love.mouse.getY() > 130 and love.mouse.getY() < 200 then
				state = 1
				initLoadingState()
			end
			if love.mouse.getX() > 285 and love.mouse.getX() < 545
				and love.mouse.getY() > 260 and love.mouse.getY() < 330 then
				love.event.push('q')
			end
		end
	elseif state == 1 then
		
	elseif state == 2 then
		if love.keyboard.isDown( "left" ) then
			x, y = bodies[1]:getLinearVelocity()
			if math.abs(x) < 350 then
				bodies[1]:applyForce(-900, 0)
			end
		end
		if love.keyboard.isDown( "right" ) then
			x, y = bodies[1]:getLinearVelocity()
			if math.abs(x) < 350 then
				bodies[1]:applyForce(900, 0)
			end
		end
		if playerY > screenH then
			state = 4
		end
		
		-- Allow magnet to attract balls
		if love.mouse.isDown( "l" ) then

		end
		
		world:update(dt)
		-- Send some updates all around
		updateMap(dt)
		updateMagnet(dt)
		updatePlayer(dt)
	elseif state == 4 then
		if love.mouse.isDown( "l" ) then
			if love.mouse.getX() > 300 and love.mouse.getX() < 430
				and love.mouse.getY() > 505 and love.mouse.getY() < 540 then
				state = 0
			end
		end
	end
end

function love.draw()

	if state == 0 then
		love.graphics.draw(menu, 0, 0)
	elseif state == 1 then
		--load
	elseif state == 2 then
		love.graphics.push()
			love.graphics.translate( - cameraX, 0 )
			-- Draw the sky
			love.graphics.draw(sky, cameraX, 0)
			-- Draw the map
			drawMap()
			-- Draw physics
			love.graphics.setColor(193, 47, 14)

			for i=0, #bodiesOfMap do
				--drawPhysics(bodiesOfMap[i])
			end
			love.graphics.setColor(255, 255, 255)
			drawBall()
			-- Draw the player
			drawPlayer()
			drawMagnet()
			-- Display FPS
			love.graphics.print("FPS: "..love.timer.getFPS(), cameraX + 10, 10)
		love.graphics.pop()
	elseif state == 4 then
		love.graphics.draw(gameover, 0, 0)
	end
end